package stackcode.leetcode;

import java.util.Stack;

/**
 * Leetcode 20题
 * 实例：1：字符串转换为字符，遍历
 *     2：遇见左括号就入栈
 *     3：取出栈顶，对比遍历的字符  判定
 */
class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c == '(' || c=='[' || c=='{') {
                stack.push(c);
            }else{
                if(stack.isEmpty()){
                    return false;
                }
                char topChar = stack.pop();

                if(c == ')' && topChar !='('){
                    return  false;
                }
                if(c == ']' && topChar !='['){
                    return  false;
                }
                if(c == '}' && topChar !='{'){
                    return  false;
                }
            }

        }
        
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        boolean valid = solution.isValid("(){}");
        System.out.println(valid);
    }
}